package com.byxy.util;

import java.sql.*;

public class DBUtil {
	private static String ip = "127.0.0.1";
	private static int port = 1433;
	private static String database = "PartyMember";
	private static String loginName = "sa";
	private static String password = "123456";

	static {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException {
		String url = String.format("jdbc:sqlserver://%s:%d;databasename=%s", ip, port, database);
		return DriverManager.getConnection(url, loginName, password);
	}

	public static void close(PreparedStatement psta, Connection conn) throws SQLException {
		if (psta != null) {
			psta.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public static void close(ResultSet rs, PreparedStatement psta, Connection conn) throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (psta != null) {
			psta.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

}
